package org.lex.ftp.ui.find;

import java.awt.event.ActionEvent;

import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

import org.lex.ftp.FTP;
import org.lex.ftp.model.FTPModel;
import org.lex.ftp.net.find.FTPFileFinder;
import org.lex.ftp.net.find.FinderObserver;
import org.lex.ftp.ui.explorer.file.FTPFileSizeRenderer;
import org.lex.ftp.ui.explorer.file.FileNameRenderer;
import org.lex.net.ftp.FTPFile;
import org.lex.net.ftp.FTPURL;
import org.lex.net.ftp.IFTPClient;
import org.lex.net.ftp.IFTPFile;
import org.lex.swing.table.DateRenderer;
import org.lex.utils.pool.DaemonExecutor;


public class FTPFileFindDialog extends BaseFindDialog implements FinderObserver {
	private static final String DEFAULT_TITLE = "Search Files on FTP ";
	private FileEntryTableModel fileEntryTableModel = null;
	private IFTPClient ftp = null;
	private FTPURL url = null;
	private String initPath = "/";
	private FTPFileFinder finder = null;

	public FTPFileFindDialog(FTP app, String initPath) {
		super(app.getRootWindow());
		FTPModel model = app.getFTPExplorer().getFTPModel();
		this.ftp = model.getFTPClient();
		this.url = model.getFTPURL();
		this.initPath = initPath;
		this.initializeComponents();
	}

	public void process(IFTPFile file) {
		this.getResultTableModel().addFileEntry(file);
	}

	public void done() {
		setStatus("Done ( " + getResultTableModel().getRowCount() + " objects found )");
		getRunButton().setEnabled(true);
		getStopButton().setEnabled(false);
		getCloseButton().setEnabled(true);
	}

	@Override
	protected void runButtonActionPerormed(ActionEvent e) {
		getRunButton().setEnabled(false);
		getStopButton().setEnabled(true);
		getCloseButton().setEnabled(false);
		// ==================================
		this.getResultTableModel().clear();
		this.finder = this.createFTPFileFinder();
		this.getGeneralPane().getProgressBar().setModel(finder);
		DaemonExecutor.execute(finder);
	}

	@Override
	protected void stopButtonActionPerformed(ActionEvent e) {
		getRunButton().setEnabled(true);
		getStopButton().setEnabled(false);
		getCloseButton().setEnabled(true);
		// ==================================
		if (null != finder)
			finder.stop();
	}

	protected void addResult(FTPFile entry) {
		this.getResultTableModel().addFileEntry(entry);
	}

	private void initializeComponents() {
		this.getGeneralPane().setHost(url.getHost());
		this.getGeneralPane().setPath(this.initPath);
		this.getStopButton().setEnabled(false);
		this.setDefaultTitle(DEFAULT_TITLE + url.getHost());
		// config resultTable
		JTable table = this.getResultTable();
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		table.setModel(this.getResultTableModel());
		int[] widths = { 180, 200, 80, 160, 80 };
		TableCellRenderer[] renderers = { new FileNameRenderer(), null, new FTPFileSizeRenderer(), new DateRenderer(), null };
		TableColumnModel model = table.getColumnModel();
		for (int i = 0; i < widths.length; i++) {
			TableColumn column = model.getColumn(i);
			column.setPreferredWidth(widths[i]);
			if (null != renderers[i])
				column.setCellRenderer(renderers[i]);
		}
	}

	private FileEntryTableModel getResultTableModel() {
		if (null == this.fileEntryTableModel) {
			this.fileEntryTableModel = new FileEntryTableModel();
		}
		return this.fileEntryTableModel;
	}

	private FTPFileFinder createFTPFileFinder() {
		if (null != finder)
			finder.stop();
		return new FTPFileFinder(this, ftp, this.initPath, this);
	}
}
